import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class code_3 {
    public int longestConsecutive(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        // 先排序,然后进行滑动
        Arrays.sort(nums);
        int n = nums.length;
        int max = Integer.MIN_VALUE;
        int flag=0;
        for (int i = 1; i < n; i++) {
            if(nums[i]-nums[i-1]==1) {
                flag++;
            } else if (nums[i-1]==nums[i]) {
                continue;
            } else {
                max = Math.max(flag,max);
                flag = 0;
            }
        }
        return Math.max(max,flag)+1;
    }

    public int longestConsecutive1(int[] nums){
        Set<Integer> set = new HashSet<>();
        int max = 0;
        // 去重
        for (int x:nums)
            set.add(x);
        for(int x:set){
            // 如果找到了就跳过
            if(!set.contains(x-1)) {
                int currnum = x;
                int flag = 1;
                // 找当前数+1有多少
                while (set.contains(currnum+1)){
                    currnum++;
                    flag++;
                }
                max = Math.max(flag,max);
            }
        }
        return max;
    }
}
